﻿using Learun.Util;
using Learun.Util.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：关联类型表
    /// </summary>
    public class ec_SofttagService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_SofttagEntity> _softtagRepository => new Repository<ec_SofttagEntity>();
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_SofttagEntity> GetList(string queryJson)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();
                //参考写法
                var queryParam = queryJson.ToJObject();
                var strSql = new StringBuilder();
                strSql.Append(@"SELECT t.* ");
                if (queryParam["ProjectId"].IsEmpty())
                {
                    return null;
                }
                else
                {
                    //ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(queryParam["ProjectId"].ToString());
                    ProjectEntity = _projectRepository.GetById(queryParam["ProjectId"].ToString());
                    strSql.Append($" FROM ec_softtag_{ProjectEntity.ProjectIndex} t ");
                }
                strSql.Append(" WHERE 1=1 ");
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["CS_TAG_NAME"].IsEmpty())
                {
                    dp.Add(new SugarParameter("CS_TAG_NAME", queryParam["CS_TAG_NAME"].ToString(), DbType.String));
                    strSql.Append(" AND t.CS_TAG_NAME = @CS_TAG_NAME ");
                }
                if (!queryParam["CS_TAG_ID"].IsEmpty())
                {
                    dp.Add(new SugarParameter("CS_TAG_ID", queryParam["CS_TAG_ID"].ToString(), DbType.String));
                    strSql.Append(" AND t.CS_TAG_ID = @CS_TAG_ID ");
                }
                if (!queryParam["Wire_Group_ID"].IsEmpty())
                {
                    dp.Add(new SugarParameter("Wire_Group_ID", queryParam["Wire_Group_ID"].ToString(), DbType.String));
                    strSql.Append(" AND t.Wire_Group_ID = @Wire_Group_ID ");
                }
                if (!queryParam["ALARM4L_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM4L_VALUE", queryParam["ALARM4L_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM4L_VALUE = @ALARM4L_VALUE ");
                }
                if (!queryParam["ALARM3L_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM3L_VALUE", queryParam["ALARM3L_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM3L_VALUE = @ALARM3L_VALUE ");
                }
                if (!queryParam["ALARM2L_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM2L_VALUE", queryParam["ALARM2L_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM2L_VALUE = @ALARM2L_VALUE ");
                }
                if (!queryParam["ALARM1L_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM1L_VALUE", queryParam["ALARM1L_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM1L_VALUE = @ALARM1L_VALUE ");
                }
                if (!queryParam["ALARM1H_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM1H_VALUE", queryParam["ALARM1H_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM1H_VALUE = @ALARM1H_VALUE ");
                }
                if (!queryParam["ALARM2H_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM2H_VALUE", queryParam["ALARM2H_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM2H_VALUE = @ALARM2H_VALUE ");
                }
                if (!queryParam["ALARM3H_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM3H_VALUE", queryParam["ALARM3H_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM3H_VALUE = @ALARM3H_VALUE ");
                }
                if (!queryParam["ALARM4H_VALUE"].IsEmpty())
                {
                    dp.Add(new SugarParameter("ALARM4H_VALUE", queryParam["ALARM4H_VALUE"].ToString(), DbType.String));
                    strSql.Append(" AND t.ALARM4H_VALUE = @ALARM4H_VALUE ");
                }
                //return this.BaseRepository().FindList<ec_SofttagEntity>(strSql.ToString(), dp);
                return SqlSugarHelper.Db.SqlQueryable<ec_SofttagEntity>(strSql.ToString()).AddParameters(dp).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 获取列表分页数据
        /// <param name="pagination">分页参数</param>
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_SofttagEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                string projectId = queryParam["ProjectId"].ToString();
                string Wire_Group_ID = queryParam["Wire_Group_ID"].ToString();
                string CS_TAG_NAME = queryParam["CS_TAG_NAME"].ToString();
                var tableName = ProjectSugar.TableName<ec_SofttagEntity>(projectId);
                var list = _softtagRepository.AsQueryable().AS(tableName)
                    .Where(x => x.Wire_Group_ID == Wire_Group_ID && x.CS_TAG_NAME.Contains(CS_TAG_NAME))
                    .ToPageList(pagination.page, pagination.rows);
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_SofttagEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                var tableName = ProjectSugar.TableName<ec_SofttagEntity>(ProjectId);
                return _softtagRepository.AsQueryable().AS(tableName).First(x => x.CS_TAG_ID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, ref string keyValue, ec_SofttagEntity entity)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                string tableName = ProjectSugar.TableName<ec_SofttagEntity>(ProjectId);
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    _softtagRepository.AsUpdateable(entity).AS(tableName).ExecuteCommand();
                }
                else
                {
                    entity.Create();
                    _softtagRepository.AsInsertable(entity).AS(tableName).ExecuteCommand();
                    keyValue = entity.CS_TAG_ID;
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                var tableName = ProjectSugar.TableName<ec_SofttagEntity>(ProjectId);
                _softtagRepository.AsDeleteable().AS(tableName).Where(x => x.CS_TAG_ID == keyValue).ExecuteCommand();
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion
    }
}
